import java.sql.*;

public class Test03 {
    public static void main(String[] args) {
        String className = "com.mysql.cj.jdbc.Driver";
        String url = "jdbc:mysql:///db2";
        String user = "root";
        String password = "123";
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;
        try {
            Class.forName(className);
            connection = DriverManager.getConnection(url, user, password);
            statement = connection.createStatement();
            String sql = "select * from stu";
            resultSet=statement.executeQuery(sql);
            while (resultSet.next()) {
                System.out.println(resultSet.getString(1));
                System.out.println(resultSet.getString("sname"));
                System.out.println(resultSet.getString(3));
                System.out.println(resultSet.getString("gender"));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }  finally {
            if(resultSet!=null){
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
            if(statement!=null){
                try {
                    statement.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
            if(connection!=null){
                try {
                    connection.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}
